var	ps_Prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

function Peerscape_Init() {
    Peerscape_CheckPrefs();
    Peerscape_UpdateUI();
}

function Peerscape_Exit() {
}

function Peerscape_DblClick(e) {
    if (e.button == 0) { // left click
        Peerscape_OpenPrefs();
    }
    e.preventDefault();
}

function Peerscape_Switch(bOn) {
    if (bOn) {
        var curPort = ps_Prefs.getIntPref('peerscape.port');
        curPAC = 'http://localhost:'+curPort+'/proxy/proxy.pac';
        ps_Prefs.setCharPref('network.proxy.autoconfig_url', curPAC);

        var curType = ps_Prefs.getIntPref('network.proxy.type');
		ps_Prefs.setIntPref('peerscape.oldtype', curType);
        ps_Prefs.setIntPref('network.proxy.type', 2);
    }
    else {
        curPAC = 'http://localhost:'+curPort+'/proxy/proxy.pac';
        var oldType = ps_Prefs.getIntPref('peerscape.oldtype');
        var oldPAC = ps_Prefs.getCharPref('peerscape.oldpac');
        ps_Prefs.setIntPref('network.proxy.type', oldType);
        ps_Prefs.setCharPref('network.proxy.autoconfig_url', oldPAC);
    }
}

Peerscape_PACRE = /^http:\/\/localhost:\d+\/proxy\/proxy.pac$/
function Peerscape_testOn() {
    curType = ps_Prefs.getIntPref('network.proxy.type')
    curPAC = ps_Prefs.getCharPref('network.proxy.autoconfig_url')

    if (curType == 2) {
        if (Peerscape_PACRE.test(curPAC)) {
            return true;
        }
    }
    return false;
}

function Peerscape_UpdateUI() {
    var psPanel = document.getElementById("peerscape-panel");
    var psOn = Peerscape_testOn();
    if (psOn) {
        var curPort = ps_Prefs.getIntPref('peerscape.port');
        psPanel.setAttribute("label", "Peerscape ["+curPort+"]");
        psPanel.setAttribute("src", "chrome://peerscape/content/fusion-16.png");
    }
    else {
        psPanel.setAttribute("label", "Peerscape [OFF]");
        psPanel.setAttribute("src", "chrome://peerscape/content/fusion-gray-16.png");
    }
}

function Peerscape_OpenPrefs() {
	window.openDialog('chrome://peerscape/content/peerscapeSettings.xul', "Peerscape Settings", 'chrome,centerscreen,dependent');
}

function Peerscape_InitPrefs(portField, onField) {
    Peerscape_CheckPrefs();
    var curPort = ps_Prefs.getIntPref('peerscape.port');
    var psOn = Peerscape_testOn();
    portField.value = curPort;
    if (psOn) {
        onField.checked = true;
    }
    else {
        Peerscape_SaveOldSettings();
        onField.checked = false;
    }
}

function Peerscape_SavePrefs(portField, onField) {
    var newPort = portField.value;
    ps_Prefs.setIntPref('peerscape.port', newPort);

    Peerscape_Switch(onField.checked);
}

function Peerscape_SaveOldSettings() {
    if (!Peerscape_testOn()) {
        var curType = ps_Prefs.getIntPref('network.proxy.type');
        var curPAC = ps_Prefs.getCharPref('network.proxy.autoconfig_url');
        ps_Prefs.setIntPref('peerscape.oldtype', curType);
        ps_Prefs.setCharPref('peerscape.oldpac', curPAC);
    }
}

function Peerscape_CheckPrefs() {
	try {
		ps_Prefs.getIntPref('peerscape.port');
	}
    catch (rErr) {
        // first time this is installed it will switch the proxy on
		ps_Prefs.setIntPref('peerscape.port', 35880);
        ps_Prefs.setIntPref('peerscape.oldtype', 0);
        ps_Prefs.setCharPref('peerscape.oldpac', '');
        Peerscape_SaveOldSettings();
        Peerscape_Switch(true);
        Peerscape_UpdateUI();
	}
}


